<!DOCTYPE HTML>
<html>
<head>
<title>BI Apps - Component Variable Screen</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="Stylesheet" type="text/css" href="../css/common/bi_app_style.css" />
<link rel="Stylesheet" type="text/css" href="../<%=theme%>/css/theme_style.css" />
<script type="text/JavaScript" src="../api/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="../api/js/jquery-ui-1.10.1.custom.min.js"></script>
<script type="text/JavaScript" src="../theme_hr/script/jquery.easing.1.3.js"></script>
<script type='text/JavaScript' src='../../zbw_reporting/scripts/framework_include.js'></script>

<!-- [REQUIRED] SCRIPT FOR INTEGRATION WITH BOI -->
<script type="text/javascript">

    //comment this line when done testing
    //$(function(){initialize();});

    var BOI = parent.BOI; //REQUIRED
    var BOILib=parent.BOILib;

    var properties = { //REQUIRED
        rep_id: undefined,
        views: {}
    };

    window.onload = function () {
        BOI.Events.addListener(BOI.Events.repchange, load);
    }

    var initialize = function initialize() {//REQUIRED //initialization function ... see below..
        /*---- begin your component custom initialization code ----*/
        //set property values to dom elements if needed
        refreshProperties();
    };
    var refresh = function refresh() { //REQUIRED function to be called by parent at anytime to refresh data.
    };
    var refreshProperties = function refreshProperties() { //REQUIRED function to be called by parent when a property change occurs
    };
</script>

<!-- COMPONENT SCRIPT -->
<script type="text/javascript">
var rep_changed = true;

$(function () {
    $('a.apply').live('click', function () {
        //get values from components
        for (var i in properties.views)

            if (properties.views[i].iframe )
                if (properties.views[i].iframe.properties) {
                    for(var j in BOI.context.reports[properties.rep_id].variables)
                      if(i==j){

                    var prop_val  = properties.views[i].iframe.properties.value;
                    var prop_values=  properties.views[i].iframe.properties.values;
                    var prop_ranges=properties.views[i].iframe.properties.ranges;
                    BOI.context.reports[properties.rep_id].variables[i].value=prop_val;
                    BOI.context.reports[properties.rep_id].variables[i].values=prop_values;
                    if(prop_ranges!=undefined)
                      if( (prop_ranges[0].from!=null) && prop_ranges[0].from!="" && prop_ranges[0].to==""){
                                prop_ranges[0].to=prop_ranges[0].from;
                                prop_ranges[0].operator = 'BT';

                            }
                    BOI.context.reports[properties.rep_id].variables[i].ranges=prop_ranges;
                    /**
                     * If it uses only signle variable screen, then update the value to the
                     * context also.
                     */

                    if(BOI.single_variable_screen){
                        BOI.context.variables[i].value = prop_val;
                    }
                    if(typeof prop_values == "object"){
                        /**
                         * If it uses only signle variable screen, then update the value to the
                         * context also.
                         */
                        if(BOI.single_variable_screen){
                            BOI.context.variables[i].values = prop_values;
                        }
                        BOI.context.reports[properties.rep_id].variables[i].values=prop_values;
                    }

                    // add for ranges

                    if (BOI.context.variables[i].seltype == "M"){
                        //  if(typeof BOI.context.variables[i].value != "string")
                        {
                            /**
                             * If it uses only signle variable screen, then update the value to the
                             * context also.
                             */
                            if(BOI.single_variable_screen){
                                BOI.context.variables[i].values = BOI.context.variables[i].value;
                            }
                            BOI.context.reports[properties.rep_id].variables[i].values= BOI.context.reports[properties.rep_id].variables[i].value;
                        }
                    }
                    // add for range (seltype="I")
                    if (BOI.context.variables[i].seltype == "I"){
                        /**
                         * If it uses only signle variable screen, then update the value to the
                         * context also.
                         */
                        if(BOI.single_variable_screen){
                            BOI.context.variables[i].ranges = properties.views[i].iframe.properties.ranges;
                            BOI.context.variables[i].values = BOI.context.variables[i].value;
                        }

                    }
                }
                }
        // Ansaj 6-March-2014 for mandatory variables
        var mandatory=0;
        for (var dep in BOI.context.reports[properties.rep_id].variables)
        {
            if (BOI.context.reports[properties.rep_id].variables[dep].ENTRYTP == "1" && BOI.context.reports[properties.rep_id].variables[dep].value == "") //MANDATORY
            {
                BOI.BOILib.Util.log("*Fill Mandatory :"+ BOI.context.reports[properties.rep_id].variables[dep].description,true);
                mandatory++;
            }

        }
        if(mandatory==0){
            BOI.Events.raiseEvent(BOI.Events.repvarchange, [properties.rep_id]);
        }

    });

    $('a.back').live('click', function () {

        parent.toggleView('tabs');
    });
    $('a.refresh').live('click', function () {
        //get values from components
        for (var i in properties.views)
        {
            //Added by jaroudnh on 2/10/2014 to break if there is no dependencies on this variable...
            if(!isDependent(properties.rep_id, BOI.context.variables[i].name))
                continue;//break;

            if (properties.views[i].iframe)
                if (properties.views[i].iframe.properties) {
                    BOI.context.variables[i].value = properties.views[i].iframe.properties.value;

                    if (BOI.context.variables[i].seltype == "M")
                        BOI.context.variables[i].values = BOI.context.variables[i].value;
                }
            BOI.loadF4Values(BOI.context.variables[i].name, 1, 50, function(data){}, [properties.rep_id]);
        }
        BOI.Events.raiseEvent(BOI.Events.varrefresh, [properties.rep_id]);
        load([properties.rep_id]);
    });

    $('a.reset').live('click', function () {
        // BOI.Events.raiseEvent(BOI.Events.varreset, [properties.rep_id]);
        for (var i in properties.views)  {
            if (properties.views[i].iframe){
                if (properties.views[i].iframe.properties) {
                    properties.views[i].properties.variable.value="";
                    properties.views[i].iframe.properties.value="";
                    properties.views[i].properties.value="";
                    $("#"+i).get(0).contentWindow.resetSelections();
                }
            }
        }
    });
    $(window).bind('resize', resize);
    $(window).bind('load', resize);
});
var load = function load(args) {
    var rep_id = args[0];
    if( properties.rep_id !=rep_id){
        rep_changed = true;
        properties.rep_id = rep_id;
    }
}
//Added by jaroudnh on 2/10/2014 to dependencies...
function isDependent(report_id, variable_id)
{
    for(var report in BOI.context.reports)
    {
        if(BOI.context.reports[report].id == report_id)
        {
            for(var variable1 in BOI.context.reports[report].variables)
            {
                //Object.keys is not supported...
                //if(Object.keys(BOI.context.reports[report].variables[variable1].dependencies).length > 0)
                var depCount = 0;
                for (var dep in BOI.context.reports[report].variables[variable1].dependencies)
                {
                    depCount++;
                }
                if(depCount > 0)
                {
                    if(variable_id == "all" || variable_id == variable1)
                        return true;
                }
            }
        }
    }
    return false;
}
function displayvariable(report){
    if (((rep_changed)&&(properties.rep_id == report.id) ) ||
            (properties.rep_id != report.id)) {
        var layout = report.varlayout;
        $('#var_screen').html('');
        if(layout!=null){
            $('#var_screen').append(layout);
            var mandatory_info = undefined;
            for (var i in report.variables) {
                var variable = report.variables[i];
                var view_id = variable.display;

                if (variable.ENTRYTP == "1" && variable.value == "") //MANDATORY
                {
                    mandatory_info += "*Fill Mandatory "+ variable.name;
                }
                else if (variable.ENTRYTP == "2" && (variable.value == variable.defval || variable.value == "")) {
                    mandatory_info += "*Fill Mandatory "+ variable.name;
                }

                if( (view_id!=undefined ) && (view_id!=null) && (view_id!='') )
                {
                    var view = jQuery.extend(true, {}, BOI.Designer.VariableViews[view_id]); //instantiate
                    //ensure setting any values from context into actual ui elements
                    view.properties.variable = variable;
                    view.properties.value = variable.value;

                    //(store references in properties.components)
                    properties.views[variable.name] = view;

                    //load their components into the layout
                    var location = $('#var_screen').find('[LOC="' + $.trim(variable.location) + '"]');
                    location.html('');

                    var iframe = $("<iframe frameborder='0' border='0' id='" + variable.name + "' name='" + variable.name + "' src='" + view.url + "'></iframe>").get(0);
                    location.append(iframe);/**/

                $(iframe).load(function () {
                    var name = $(this).attr('name');
                    if (window.frames[name])
                        if (window.frames[name].document.readyState == "complete") {
                            properties.views[name].element = $(this);
                            properties.views[name].iframe = window.frames[name];
                            //  this.contentWindow.properties.variable = BOI.context.variables[name];
                            this.contentWindow.properties.variable = BOI.context.reports[report.id].variables[name];
                            // this.contentWindow.properties.value = BOI.context.variables[name].value;
                            this.contentWindow.properties.value =  BOI.context.reports[report.id].variables[name].value;
                            this.contentWindow.initialize();
                        }
                });
                }
                //Removing empty DIV
                var location = $('#var_screen').find('[LOC="' + $.trim(variable.location) + '"]');
                if((location = undefined) && (location = null) && (location = '')){
                    $(location).hide();
                    alert('hide')
                }
            }
        }
        properties.rep_id = report.id;
        rep_changed = false;
    }
    //Added by jaroudnh on 2/10/2014 to hide refresh button if no dependencies...
    if(!isDependent(properties.rep_id, "all"))
        $('#ref_btn').hide();
    else
        $('#ref_btn').show();
    //-----------------------------------------------
}

var resize = function () {
}
var html;
var submit = function submit() {
    var report = BOI.context.reports[properties.rep_id];
    //read variable values from each component
    for (var i in properties.components) {
        var comp = properties.components[i];
        var varname = comp.varname;
        var value = comp.value;
        report.variables[varname].value = value;
    }
    //notify that the report should be reloaded with new values....
    BOI.Events.raiseEvent(BOI.Events.repvarchange, [properties.rep_id]); //send args as array: [rep_id]
}
</script>
<style>
    body { overflow:hidden; background: none;}
</style>
</head>
<body>
<div class="varscreen">
    <div id="title_container"> <!--h2 class="varscreen_title"> </h2--> </div>
    <div id="var_screen" class="biapp_var_screen">
        <!--<table style="width: 100%; height: 100%;" var_grp="0">
            <tbody>
                <tr>
                    <td style="width: 67%; height: 33%;" colspan="2" loc="0">
                        <iframe id="X_HRROLE" src="comp-organization.htm" name="X_HRROLE"></iframe>
                    </td>
                    <td style="width: 33%; height: 67%;" rowspan="2" loc="1">
                        <iframe id="X_ROLOBY" src="comp-organization.htm" name="X_ROLOBY"></iframe>
                    </td>
                </tr>
                <tr>
                    <td style="width: 33%; height: 33%;" loc="2">
                        <iframe id="X_HRSDOG" src="comp-organization.htm" name="X_HRSDOG"></iframe>
                    </td>
                    <td style="width: 33%; height: 33%;" loc="3">
                        <iframe id="X_ACUSID" src="comp-organization.htm" name="X_ACUSID"></iframe>
                    </td>
                </tr>
                <tr>
                    <td style="width: 33%; height: 33%;" loc="5">
                        <iframe id="X_ACSYST" src="comp-organization.htm" name="X_ACSYST"></iframe>
                    </td>
                    <td style="width: 33%; height: 33%;" loc="6"></td>
                    <td style="width: 33%; height: 33%;" loc="7"></td>

                </tr>

            </tbody>
        </table>-->
    </div>
</div>
<div class="controls">
    <ul>
        <li id="ref_btn"><a class="refresh button-content-YW"><span>Refresh</span></a></li>
        <li><a class="back button-content-YW"><span>Back</span></a></li>
        <li><a class="apply button-content-BW"><span>Apply</span></a></li>
        <li><a class="reset button-content-WB"><span>Reset</span></a></li>
    </ul>
</div>
</body>
</html>